// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import utils from './components/public/utils'
import router from './router'
import VueClipboard from 'vue-clipboard2'
import Vuex from 'vuex'
import store from './store/store'
import axios from 'axios'
import md5 from 'js-md5';
import Share from 'vue-social-share'
import 'vue-social-share/dist/client.css';
import { Overlay ,Field ,Popup,Switch ,Toast,Progress,CountDown,Checkbox,Swipe, SwipeItem } from 'vant';

Vue.prototype.$md5 = md5;
Vue.prototype.$http = axios;
Vue.prototype.utils = utils;
Vue.prototype.$addStorageEvent = function (key, data) {
  // 创建一个StorageEvent事件
  var newStorageEvent = document.createEvent('StorageEvent');
  const storage = {
      setItem: function (k, val) {
          localStorage.setItem(k, val);
          // 初始化创建的事件
          newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
          // 派发对象
          window.dispatchEvent(newStorageEvent);
      }
  }
  return storage.setItem(key, data);
}
// Vue.prototype.listenerBackbutton = function (){
//   var time = new Date().getTime();  // 用来存上一次按键时间；
//   setTimeout(() => {
//       // 监听返回按钮
//       document.addEventListener('backbutton', function (evt) {
//           // var url = location.hash.split('/')[1];
//           var url = router.history.current.path;
//           if (url === '/') {// 处于app首页、登录页,满足退出app操作
//               if (new Date().getTime() - time < 2000) {// 小于2s,退出程序
//                   //navigator.app.exitApp();//cordova工具时调用的
//                   plus.runtime.quit();//hbuild工具时调用的
//               } else {   // 大于2s，重置时间戳，
//                   time = new Date().getTime();
//                   // Toast('再次点击退出', 2000);
//               }
//               return;
//           } else {
//               // history.back();  // 不满足退出操作，，返回上一页
//               if(localStorage.getItem('returnVal') == 'false'){
//                 this.$addStorageEvent('returnVal','true');
//               }else{
//                 history.back();
//               }
//           }
//       }, false);
//   }, 10)
// }

Vue.directive('focus', {
  // 当被绑定的元素插入到 DOM 中时……
  inserted: function (el) {
    // 聚焦元素
    el.focus()
  }
})
// router.afterEach((to, from, next) => {
//   document.documentElement.scrollTop = document.body.scrollTop = 0;
//   window.scrollTo(0, 0);
// })
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0)
});
axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('token')) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers['X-Access-Token'] = `${localStorage.getItem('token')}`;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });
  
  
Vue.use(Share)
Vue.use(Overlay).use(Field).use(Popup).use(Switch).use(Toast).use(Progress).use(CountDown).use(Checkbox).use(Swipe).use(SwipeItem);
Vue.use(Vuex)
Vue.use(VueClipboard)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
